import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import { Https } from '../../app/http.service'

import * as city from '../../assets/datas/chinese-cities';
/**
 * Generated class for the AddBankTwoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-add-bank-two',
  templateUrl: 'add-bank-two.html',
})
export class AddBankTwoPage {
	Url: string = new Https().Url + "act=goods&op=addBank"; 
	private cityColumns: any;
	chioceAddress: string;
	man:string;
	address = [];
	cardId:string;
	bankName:string
	constructor(public navCtrl: NavController, public navParams: NavParams, public toastCtrl: ToastController) {
		this.man = navParams.get('man');
		this.cardId = navParams.get('cardId');
		this.bankName = navParams.get('bankName');
		this.cityColumns = city.city;
		console.log(this.bankName)
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad AddBankTwoPage');
		
	}
	//取消地址选择
	cancel($event) {

	}
	//地址选择
	change(event, dom) {
		this.address = []
		let arr = '';
		// Object.values(event)
		for (let i in event) {
			this.address.push(event[i])
		}
		for (let item of this.address) {
			arr += item.text + " ";
		}
		dom.innerHTML = arr;
		this.chioceAddress = arr;
		arr = '';
		console.log(this.address);
	}
	//添加银行卡
    // man : 开户人名称
    // cardid : 银行卡号
    // hang：开户行
    // province : 省
    // city ： 城市
    // area : 区
    // name : 银行名
	next(name,hang){
		if (name != '' && hang != '' && this.address != null){
			let then = this;
			//如果直辖市 
			let province = this.address[0].text;
			let city = this.address[1].text;
			if (this.address[0].value == '110000' || this.address[0].value == '120000' || this.address[0].value == '310000' || this.address[0].value == '500000') {
				city = this.address[2].text
			}
			fetch(this.Url, {
				method: "post",
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&man=" + this.man + "&cardid=" + this.cardId + "&hang=" + hang + "&name=" + name + "&province=" + province + "&city=" + city,
			}).then((responsce) => {
				responsce.text().then((data) => {
					if (data != '') {
						let json = JSON.parse(data).datas;
						if (json.code == '0') {
							then.presentToast('添加成功');
							then.navCtrl.pop();
						} else {
							then.presentToast('添加');
						}
					} else {
						then.presentToast('网络错误');
					}

				})

			})
		}else{
			this.presentToast('信息填写不全')
		}
		
	}
	presentToast(content) {
		let toast = this.toastCtrl.create({
			message: content,
			duration: 3000
		});
		toast.present();
	}
}
